package com.google.code.tretris.rendering;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;


public class Square extends MVObject {
	
	
	private final FloatBuffer vertices;
	private final FloatBuffer normals = BufferUtil.wrap(new float[] { 0, 0, 1,
			0, 0, 1, 0, 0, 1, 0, 0, 1, });
	private final ShortBuffer indices = BufferUtil.wrap(new short[] { 0, 1, 2,
			0, 2, 3 });

	public Square(final float width) {
		x = y = z = rx = ry = rz = red = green = blue = 0;
		sx = sy = sz = 1.0f ;
		float w = width / 2;
		vertices = BufferUtil.wrap(new float[] { -w, -w, 0, w, -w, 0, w, w, 0,
				-w, w, 0 });
	}
	
	public void setPosition(final float x, final float y, final float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public void setRotationX(final float rx) {
		this.rx = rx;
	}

	public void setRotationY(final float ry) {
		this.ry = ry;
	}

	public void setRotationZ(final float rz) {
		this.rz = rz;
	}

	public void setRed(final float red) {
		this.red = red;
	}

	public void setGreen(final float green) {
		this.green = green;
	}

	public void setBlue(final float blue) {
		this.blue = blue;
	}

	public void setScaleX(final float sx) {
		this.sx = sx;
	}

	public void setScaleY(final float sy) {
		this.sy = sy;
	}

	public void setScaleZ(final float sz) {
		this.sz = sz;
	}
	
	@Override
	protected void renderShape(GL10 gl) {
		gl.glEnable(GL10.GL_COLOR_MATERIAL);

		gl.glNormalPointer(GL10.GL_FLOAT, 0, normals);
		gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);

		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		
		gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indices);
		
		normals.rewind();
		vertices.rewind();
		indices.rewind();
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
	}

}
